package com.phospherus.flow.service

import com.phospherus.common.service.impl.BaseServiceImpl
import com.phospherus.flow.entity.WorkFlowInst
import com.phospherus.flow.repository.WorkFlowInstRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/22
 */
@Service
class WorkFlowInstService : BaseServiceImpl<WorkFlowInst, Long, WorkFlowInstRepository>() {
    @Autowired
    lateinit var flowNodeService: FlowNodeService

    fun createWorkFlowInst(map: MutableMap<String, Any>): WorkFlowInst {
        var currNodeId = map["currNodeId"] as Long
        var formId = (map["formId"] as String).toLong()
        var flowId = (map["flowId"] as String).toLong()
        var formInstId = map["formInstId"] as Long
        var workFlowType = map["workFlowType"] as Int?
        var finishTime = map["finishTime"] as Date?
        var currUserName = map["currUserName"] as String?
        var currUserId = map["currUserId"] as Long?
        var beforeWorkFlowInstId = map["beforeWorkFlowInstId"] as Long?

        var workFlowInst = WorkFlowInst()
        save(with(workFlowInst) {
            this.flowId = flowId
            this.formId = formId
            this.formInstId = formInstId
            this.currUserName = currUserName
            this.currUserId = currUserId
            this.currNodeId = currNodeId
            this.arrivalTime = Date()
            this.finishTime = finishTime
            this.workFlowType = workFlowType ?: 1
            this.beforeWorkFlowInstId = beforeWorkFlowInstId
            this
        })
        return workFlowInst
    }

    fun getCurrWorkFlowInstByFromInstId(formInstId: Long): WorkFlowInst? = repository.getCurrWorkFlowInstByFromInstId(formInstId)
}